### Replication for Plots for the paper:
### "UN peacekeeping upon deployment - Peacekeeping activities in theory and practice"
### Otto, Sabine., Kube, Felix., & Smidt, Hannah. (2024). UN peacekeeping upon deployment: Peacekeeping activities in theory and practice. Cooperation and Conflict, 0(0). https://doi.org/10.1177/00108367241235888
### 22/01/2024
##version:  R version 4.3.2 (2023-10-31)
  

##set directory
setwd("/Users/sor/Dropbox/VR/Internship/Felix-IntershipCivilianPKO/Cooperation and Conflict_Replication Files")

# base plot 
base  <- ggplot() + 
  theme_bw(base_size = 14)



######Figure 2
#read data figure 2
Figure2 <- readRDS(file = "Figure2.rds")

#plot figure 2
Figure2_plot <- base +
  geom_bar(aes(y = diff, x = date), stat = "identity", data = Figure2) +
  scale_x_date(date_breaks = "5 years", date_labels = "%Y") +
  xlab("") +
  ylab("Average number of activities")


#####FIGURE 3

##read data for heat map (Figure 3)
Figure3<- readRDS(file = "heatmap.rds")


#plot Figure 3
Figure3_plot <- base +
  geom_bin_2d(aes(x = reorder(PKO, order), y = reorder(Activity,-impl), fill = Implemented), data = Figure3, 
              show.legend = FALSE, size = 5) +
  xlab("") + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1)) +
  scale_fill_discrete(type = c("grey", "black")) +
  facet_col(~ factor(Category, levels= c("Rights-based", "Institutional PB", "People-centered PB", "State-centered PB", 
                                         "Offensive use of force","Protection of civilians","Classical security" )),
            space = "free", scales = "free_y")+
  ylab("")

######Figure 4 
##read data for heat map (Figure 3)
Figure4<-readRDS(file = "Figure4.rds")

#needed for plotting to calculate share - see describtin o hsare calulation in paper
Figure4_yearmon<- readRDS(file = "data_replication/Figure4_yearmon.rds")



normalizer2 <- max(Figure4_yearmon$V1) / max(Figure4$perc)

#Plot Figure 4
Figure4_plot <- base +
  geom_smooth(aes(x = date, y = perc, linetype = Activity, group = Activity, color = Activity), se = FALSE, data = Figure4, n = 344) +
  scale_x_date(date_breaks = "3 years", date_labels = "%Y",limits = c(as.Date("01/01/1989", "%d/%m/%Y"), as.Date("31/12/2018", "%d/%m/%Y"))) + 
  ylab("Share of activities within each category") +
  theme(legend.position="top",legend.title = element_blank()) +
  scale_linetype_manual(values = c("solid", "longdash", "dotted", "dotdash", "solid", "longdash", "dotted")) +
  scale_color_manual(values = c("black", "black", "black", "black", "blue", "blue", "blue")) +
  xlab("")  +
  geom_bar(aes(x = month, y = V1 / normalizer2, alpha = 0.3), width = 15, stat = "identity", data = Figure4_yearmon, show.legend = FALSE) +
  scale_y_continuous(sec.axis = sec_axis(trans = ~.*normalizer2,
                                         name = "Number of active UNPKOs"),
                     limits = c(0,1))


##########################Figure 5###################

#read data for figure 5

Figure5<-readRDS(file = "Figure5.rds")

#plot figure 5

gen_names <- as_labeller(
  c(
    `Second generation` = "Second generation \n (1988-1998)",
    `Third generation` = "Third generation \n (1999-2009)",
    `Stabilization` = "Stabilization \n (2010-present)"
  )
)

Figure5_plot <- base +
  geom_smooth(aes(x = month_index, y = perc, group = Activity, colour = Activity, linetype = Activity), data = Figure5 %>% filter(month_index <= 24), se = FALSE, n = 93) +
  ylab("Share of activities within each category") +
  xlab("Months since UNPKO start") +
  scale_linetype_manual(values = c("solid", "solid")) +
  scale_color_manual(values = c("blue", "black")) +
  theme(legend.position="top",legend.title = element_blank()) +
  facet_wrap(~ factor(gen,levels = c("Second generation", "Third generation", "Stabilization")), scales = "fixed", labeller = gen_names)



########################Appendix###########################
#read data appdenix
Figureappendix<-readRDS(file = "Figureappendix.rds")

#plot appendix

PKO_order <- Figureappendix %>% arrange(lower) %>% .$PKO %>% str_remove("\\*")

Figureappendix_plot <- base +
  geom_linerange(aes(xmin = lower, xmax = upper, y = reorder(PKO, lower)), 
                 data = Figureappendix,
                 size = 2) +
  scale_x_continuous(n.breaks = 10) +
  facet_col(~ Continent, scales = "free_y", space = "free") +
  ylab("")
